package com.avengers.core.thread;

import com.avengers.job.JobNoKit;

import java.util.concurrent.*;

/**
 * @author yu.wang
 * @since 2020-08-13 15:05
 **/
public class ScheduledAvengersThreadPoolExecutor extends ScheduledThreadPoolExecutor {
    public ScheduledAvengersThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize);
    }

    public ScheduledAvengersThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {
        super(corePoolSize, threadFactory);
    }

    public ScheduledAvengersThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) {
        super(corePoolSize, handler);
    }

    public ScheduledAvengersThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, threadFactory, handler);
    }

    @Override
    public void execute(Runnable command) {
        String jobNo = JobNoKit.get();
        super.execute(() -> {
            JobNoKit.set(jobNo);
            command.run();
        });
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        String jobNo = JobNoKit.get();
        return super.submit(() -> {
            JobNoKit.set(jobNo);
            return task.call();
        });
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        String jobNo = JobNoKit.get();
        return super.submit(() -> {
            JobNoKit.set(jobNo);
            task.run();
        }, result);
    }

    @Override
    public Future<?> submit(Runnable task) {
        String jobNo = JobNoKit.get();
        return super.submit(() -> {
            JobNoKit.set(jobNo);
            task.run();
        });
    }
}
